package com.android.readcard.utils.djlm.DJLMReader3.reader;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Sabirjan on 2017/10/12.
 * DJLMReader.
 * 飞宏电子
 */

public class CommandTools {

    private static final String TAG = "蓝牙";
    private static ArrayList<String> commandList = new ArrayList<>();
    //内部验证命令
    public static final String[] SeriesOfCommandsFromSelectToAuthen = {"050000", "1D0000000000080108", "00A40000026002", "80B0000010", "80B0001010"};


    //构造二进制文件读取的指令列表
    public static ArrayList<String> getCommandList() {
        if (commandList != null) commandList.clear();
        commandList = new ArrayList<>();
        AddReadBinFileCommandList(commandList, 0x6011, 214, 16);
        AddReadBinFileCommandList(commandList, 0x6012, 256, 16);
        AddReadBinFileCommandList(commandList, 0x6013, 1024, 16);
    //    AddReadBinFileCommandList(commandList, 0x6021, 1024, 16);

//        for(int i=0;i<commandList.size();i++)
//        {
//            Log.i(TAG, "getCommandList: "+commandList.get(i)+"  i="+i);
//        }


        return commandList;
    }


    //构造二进制文件读取的指令列表
    private static void AddReadBinFileCommandList(List<String> commandList, int nFileID, int nLen, int nPerLen) {
        int nReadLen = 0;
        commandList.add(Make_SELECT_FileCommand(nFileID));
        while (nReadLen < nLen) {
            int nThisReadLen = nLen - nReadLen;
            if (nThisReadLen > nPerLen)
                nThisReadLen = nPerLen;
            commandList.add(Make_READ_BINARY_Command(nReadLen, nThisReadLen));
            nReadLen += nThisReadLen;
        }
    }

    //构造COS文件选择指令
    private static String Make_SELECT_FileCommand(int nFileID) {
        byte[] query = new byte[]{0x00, (byte) 0xA4, 0x00, 0x00, 0x02, 0x60, 0x02};
        query[5] = (byte) ((nFileID >> 8) & 0xff);
        query[6] = (byte) ((nFileID) & 0xff);
        return HexUtil.bytesToHexString(query);
    }

    //构造COS文件二进制读取指令
    private static String Make_READ_BINARY_Command(int nPos, int nLen) {
        byte[] query = new byte[]{(byte) 0x80, (byte) 0xB0, 0x00, 0x00, 0x00};
        query[2] = (byte) ((nPos >> 8) & 0xff);
        query[3] = (byte) ((nPos) & 0xff);
        query[4] = (byte) nLen;
        return HexUtil.bytesToHexString(query);
    }
}
